package com.fr.report.painter;

import com.fr.base.FRContext;
import com.fr.base.Inter;
import com.fr.base.background.Background;
import com.fr.base.background.ColorBackground;
import com.fr.base.core.Converter;
import com.fr.data.core.db.BinaryObject;
import com.fr.report.CellElement;
import com.fr.report.FloatElement;
import com.fr.report.ReportHF;
import com.fr.report.ReportPage;
import com.fr.report.ReportSettings;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.CellPageAttr;
import com.fr.report.cellElement.CoverCellElement;
import com.fr.report.cellElement.Formula;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.PaintUtils;
import com.fr.report.core.ReportHelper;
import com.fr.report.core.ReportUtils;
import com.fr.util.Utils;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import java.awt.geom.Rectangle2D.Double;
import java.sql.Blob;
import java.sql.Clob;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PagePainter
  implements Converter
{
  private Graphics2D g2d;
  private boolean isPrint;
  private ReportPage reportPage;

  public PagePainter(ReportPage paramReportPage, Graphics2D paramGraphics2D, boolean paramBoolean)
  {
    this.reportPage = paramReportPage;
    this.g2d = paramGraphics2D;
    this.isPrint = paramBoolean;
  }

  public void convert()
  {
    preparePaint();
    paintPage();
    this.g2d.dispose();
  }

  private void paintPage()
  {
    DynamicValueList localDynamicValueList1 = ReportHelper.createColumnWidthList(this.reportPage);
    DynamicValueList localDynamicValueList2 = ReportHelper.createRowHeightList(this.reportPage);
    prepareCell(this.reportPage.cellIterator(), localDynamicValueList1, localDynamicValueList2);
    prepareFloat(this.reportPage.floatIterator(), localDynamicValueList1, localDynamicValueList2);
  }

  private void preparePaint()
  {
    ReportSettings localReportSettings = this.reportPage.getReportSettings();
    int i = this.reportPage.getPageWidth();
    int j = this.reportPage.getPageHeight();
    int k = this.reportPage.getMarginTop();
    int l = this.reportPage.getMarginLeft();
    int i1 = this.reportPage.getMarginBottom();
    int i2 = this.reportPage.getMarginRight();
    if ((i > 100000.0D) || (j > 100000.0D) || (i * j > 400000000.0D))
      FRContext.getLogger().log(Level.INFO, Inter.getLocText("Page_OutOfLimit_Msg"));
    if (this.isPrint)
    {
      double d = 72.0D / localReportSettings.getResolution();
      this.g2d.scale(d, d);
    }
    else
    {
      ColorBackground.getInstance(Color.white).paint(this.g2d, new Rectangle2D.Double(0.0D, 0.0D, i, j));
    }
    int i3 = i - l - i2;
    prepareHF(i3, localReportSettings);
    prepareContentArea(this.g2d, new Rectangle2D.Double(l, k + ReportUtils.getHeaderHeight(localReportSettings), i3, j - k - i1 - ReportUtils.getHeaderHeight(localReportSettings) - ReportUtils.getFooterHeight(localReportSettings)), localReportSettings, this.isPrint);
  }

  protected void prepareHF(int paramInt, ReportSettings paramReportSettings)
  {
    ReportHF localReportHF1 = this.reportPage.getHeader();
    if (localReportHF1 != null)
      localReportHF1.paint(this.g2d, new Rectangle2D.Double(this.reportPage.getMarginLeft(), this.reportPage.getMarginTop(), paramInt, ReportUtils.getHeaderHeight(paramReportSettings)), paramInt, ReportUtils.getHeaderHeight(paramReportSettings), this.reportPage.getCurrentPageNumber(), this.reportPage.getTotalPages(), this.isPrint);
    ReportHF localReportHF2 = this.reportPage.getFooter();
    if (localReportHF2 != null)
      localReportHF2.paint(this.g2d, new Rectangle2D.Double(this.reportPage.getMarginLeft(), this.reportPage.getPageHeight() - this.reportPage.getMarginBottom() - ReportUtils.getFooterHeight(paramReportSettings), paramInt, ReportUtils.getFooterHeight(paramReportSettings)), paramInt, ReportUtils.getFooterHeight(paramReportSettings), this.reportPage.getCurrentPageNumber(), this.reportPage.getTotalPages(), this.isPrint);
  }

  private void prepareContentArea(Graphics2D paramGraphics2D, Rectangle2D paramRectangle2D, ReportSettings paramReportSettings, boolean paramBoolean)
  {
    this.g2d = ((Graphics2D)paramGraphics2D.create());
    this.g2d.translate(paramRectangle2D.getX(), paramRectangle2D.getY());
    Background localBackground = paramReportSettings.getBackground();
    if (localBackground != null)
      if (paramBoolean)
        if (paramReportSettings.isPrintBackground())
          localBackground.paint(this.g2d, new Rectangle2D.Double(0.0D, 0.0D, paramRectangle2D.getWidth(), paramRectangle2D.getHeight()));
      else if (paramReportSettings.isPreviewBackground())
        localBackground.paint(this.g2d, new Rectangle2D.Double(0.0D, 0.0D, paramRectangle2D.getWidth(), paramRectangle2D.getHeight()));
    int i = 0;
    int j = 0;
    for (int k = 0; k < this.reportPage.getColumnCount(); ++k)
      i += this.reportPage.getColumnWidth(k);
    for (k = 0; k < this.reportPage.getRowCount(); ++k)
      j += this.reportPage.getRowHeight(k);
    if (i <= 0)
      i = 1;
    if (j <= 0)
      j = 1;
    k = 0;
    int l = 0;
    if (paramReportSettings.isHorizontalCenterOnPage())
      k = (int)(paramRectangle2D.getWidth() - i) / 2;
    if (paramReportSettings.isVerticalCenterOnPage())
      l = (int)(paramRectangle2D.getHeight() - j) / 2;
    this.g2d.translate(k, l);
  }

  public void prepareCell(Iterator paramIterator, DynamicValueList paramDynamicValueList1, DynamicValueList paramDynamicValueList2)
  {
    Object localObject1;
    Object localObject2;
    Rectangle localRectangle1;
    Object localObject3;
    ArrayList localArrayList1 = new ArrayList();
    ArrayList localArrayList2 = new ArrayList();
    ArrayList localArrayList3 = new ArrayList();
    ArrayList localArrayList4 = new ArrayList();
    while (paramIterator.hasNext())
    {
      Rectangle localRectangle2;
      CellElement localCellElement = (CellElement)paramIterator.next();
      CellGUIAttr localCellGUIAttr = localCellElement.getCellGUIAttr();
      if (localCellGUIAttr == null)
        localCellGUIAttr = CellGUIAttr.DEFAULT_CELLGUIATTR;
      localRectangle1 = new Rectangle(localCellElement.getColumn(), localCellElement.getRow(), localCellElement.getColumnSpan(), localCellElement.getRowSpan());
      localObject3 = localCellElement.getCellPageAttr();
      if (localObject3 == null)
        localObject3 = CellPageAttr.DEFAULT_CELLPAGEATTR;
      if ((localRectangle2 = ((CoverCellElement)localCellElement).getCellPaintRect()) != null)
        localObject1 = (Rectangle)localRectangle2;
      else
        localObject1 = new Rectangle(paramDynamicValueList1.getRangeValue(0, localRectangle1.x), paramDynamicValueList2.getRangeValue(0, localRectangle1.y), paramDynamicValueList1.getRangeValue(localRectangle1.x, localRectangle1.x + localRectangle1.width), paramDynamicValueList2.getRangeValue(localRectangle1.y, localRectangle1.y + localRectangle1.height));
      if ((localRectangle2 = ((CoverCellElement)localCellElement).getCellClipRect()) != null)
        localObject2 = (Rectangle)localRectangle2;
      else
        localObject2 = new Rectangle(0, 0, ((Rectangle)localObject1).width, ((Rectangle)localObject1).height);
      localArrayList1.add(localCellElement);
      localArrayList2.add(localCellGUIAttr);
      localArrayList3.add(localObject1);
      localArrayList4.add(localObject2);
      if (((this.isPrint) && (localCellGUIAttr.isPrintBackground())) || ((!(this.isPrint)) && (localCellGUIAttr.isPreviewBackground())))
        PaintUtils.paintBackground(this.g2d, localCellElement.getStyle(), (Rectangle)localObject1, (Rectangle)localObject2);
      paintCellContent(localCellGUIAttr, this.isPrint, localCellElement, (Rectangle)localObject1, (Rectangle)localObject2);
    }
    int i = 0;
    int j = localArrayList1.size();
    while (i < j)
    {
      localObject1 = (CellElement)localArrayList1.get(i);
      localObject2 = (CellGUIAttr)localArrayList2.get(i);
      localRectangle1 = (Rectangle)localArrayList3.get(i);
      localObject3 = (Rectangle)localArrayList4.get(i);
      if (((this.isPrint) && (((CellGUIAttr)localObject2).isPrintBorder())) || ((!(this.isPrint)) && (((CellGUIAttr)localObject2).isPreviewBorder())))
        PaintUtils.paintBorder(this.g2d, ((CellElement)localObject1).getStyle(), localRectangle1, (Rectangle)localObject3);
      ++i;
    }
  }

  protected void paintCellContent(CellGUIAttr paramCellGUIAttr, boolean paramBoolean, CellElement paramCellElement, Rectangle paramRectangle1, Rectangle paramRectangle2)
  {
    if ((!(paramBoolean)) || (paramCellGUIAttr.isPrintContent()))
    {
      if ((localObject = ((CoverCellElement)paramCellElement).getCellOriValue()) == null);
      for (Object localObject = paramCellElement.getValue(); localObject instanceof Formula; localObject = ((Formula)localObject).getResult());
      if ((!(paramCellGUIAttr.isPrintZeroValue())) && (localObject != null) && (localObject.toString().matches("[\\-]?0[\\.]?[0]*")))
        return;
      if (localObject instanceof Blob)
        localObject = Utils.blob2Object((Blob)localObject, paramCellGUIAttr.isShowAsImage());
      if (localObject instanceof Clob)
        localObject = Utils.clob2String((Clob)localObject);
      if (localObject instanceof BinaryObject)
        if (paramCellGUIAttr.isShowAsImage())
          localObject = ((BinaryObject)localObject).getImage();
        else
          localObject = ((BinaryObject)localObject).getValue();
      if ((localObject instanceof String) && (paramCellGUIAttr.isShowAsHTML()))
        localObject = PaintUtils.createHTMLContentBufferedImage((String)localObject, paramRectangle1);
      PaintUtils.paintContent(this.g2d, localObject, paramCellElement.getStyle(), paramRectangle1, paramRectangle2);
    }
  }

  public void prepareFloat(Iterator paramIterator, DynamicValueList paramDynamicValueList1, DynamicValueList paramDynamicValueList2)
  {
    while (true)
    {
      FloatElement localFloatElement;
      Rectangle localRectangle2;
      Rectangle localRectangle3;
      while (true)
      {
        do
        {
          if (!(paramIterator.hasNext()))
            return;
          localFloatElement = (FloatElement)paramIterator.next();
          int i = localFloatElement.getColumn();
          int j = localFloatElement.getRow();
          Rectangle localRectangle1 = new Rectangle(paramDynamicValueList1.getRangeValueFromZero(i) + localFloatElement.getLeftDistance(), paramDynamicValueList2.getRangeValueFromZero(j) + localFloatElement.getTopDistance(), localFloatElement.getSize().width, localFloatElement.getSize().height);
          localRectangle2 = null;
          localRectangle3 = null;
          localRectangle2 = new Rectangle(localRectangle1.x, localRectangle1.y, localRectangle1.width, localRectangle1.height);
          localRectangle3 = new Rectangle(0, 0, localRectangle2.width, localRectangle2.height);
        }
        while (localRectangle2 == null);
        if (localRectangle3 != null)
          break;
      }
      PaintUtils.paintFloatElement(this.g2d, localFloatElement, localRectangle2, localRectangle3);
    }
  }
}